/* 
 * File:   JAdapterRecv.cpp
 * Author: johnneyshi
 * 
 * Created on 2013年3月5日, 上午11:58
 */

#include "JAdapterRecv.h"
#include <pthread.h>
extern pthread_mutex_t recvMsgWorkerFdsLock;

JAdapterRecv::JAdapterRecv(AdapterThreadArg* _pArg) : pArg(_pArg)
{
    
}

void JAdapterRecv::Start()
{
    int toWorkerConnFd = -1,toMasterConnFd = -1,uuidLen = -1;
    char strUuid[100];
    while(true)
    {
        toWorkerConnFd = GetToMasterConnFd();
        //读取toMasterConnFd
        read(toWorkerConnFd,(void*)&toMasterConnFd,SIZEOF_INT);
        read(toWorkerConnFd,(void*)&uuidLen,SIZEOF_INT);
        read(toWorkerConnFd,(void*)strUuid,uuidLen);
        
    }
    
}

int JAdapterRecv::GetToMasterConnFd()
{
    
}

int JAdapterRecv::GetToWorkerConnFd()
{
    int toWorkerConnFd = -1;
    std::vector<HasMsgFd>& recvMsgWorkerFds = pArg->pMaster->recvMsgWorkerFds;
    while(true)
    {
        pthread_mutex_lock(&recvMsgWorkerFdsLock);
        for(int i = 0;i < recvMsgWorkerFds.size(); ++i)
        {
            if(recvMsgWorkerFds[i].hasMsg == true && recvMsgWorkerFds[i].uniqueFd.isInDeal == false)
            {
                recvMsgWorkerFds[i].uniqueFd.isInDeal = true;
                toWorkerConnFd = recvMsgWorkerFds[i].uniqueFd.fd;
                break;
            }
        }
        pthread_mutex_unlock(&recvMsgWorkerFdsLock);
        if(toWorkerConnFd == -1)
        {
            usleep(10);
        }
        else
        {
            break;
        }
    }
    return toWorkerConnFd;
}

JAdapterRecv::~JAdapterRecv() 
{
}

